#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "levelselectwidget.h"

#include <QTime>
#include <QMessageBox>

MainWindow::MainWindow(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    this->setWindowTitle(tr("game"));
    m_timer = new QTimer(this);
    m_timer->setInterval(1000);

    // 关联信号槽
    connect(m_timer, SIGNAL(timeout()), this, SLOT(SltTimeCount()));
    connect(ui->widgetGamePanel, SIGNAL(signalWin()), this, SLOT(SltWinTheGame()));
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_btnStart_clicked()
{
    if (m_timer->isActive()) {
        m_timer->stop();
        ui->widgetGamePanel->Stop();
    }
    else {
        m_nTimeCnts = 0;
        m_timer->start();
        ui->widgetGamePanel->Start();
    }
}

void MainWindow::SltTimeCount()
{
    m_nTimeCnts++;

    quint8 nHour = (m_nTimeCnts / 3600);
    quint8 nMin = (m_nTimeCnts % 3600) / 60;
    quint8 nSec = (m_nTimeCnts % 3600) % 60;
    ui->labelTimes->setText(QString("%1:%2:%3").arg(nHour, 2, 10, QChar('0')).arg(nMin, 2, 10, QChar('0')).arg(nSec, 2, 10, QChar('0')));
}

void MainWindow::SltWinTheGame()
{
    if (m_timer->isActive()) m_timer->stop();

    //quint8 nHour = (m_nTimeCnts / 3600);
    //quint8 nMin = (m_nTimeCnts % 3600) / 60;
   // quint8 nSec = (m_nTimeCnts % 3600) % 60;

    //QString strTimes = QString("%1:%2:%3").arg(nHour, 2, 10, QChar('0')).arg(nMin, 2, 10, QChar('0')).arg(nSec, 2, 10, QChar('0'));

    QMessageBox::information(this, tr("succeed"), tr("congratulations to you"));
}

void MainWindow::on_btnLevelSelect_clicked()
{

    LevelSelectWidget *levelDialog = new LevelSelectWidget(this);
    levelDialog->exec();
    int nlevel = levelDialog->GetLevel();
    if (-1 != nlevel && nlevel < 4) {
        if (m_timer->isActive()) m_timer->stop();
        ui->widgetGamePanel->SetR(3 + nlevel);
    }

    delete levelDialog;
    levelDialog = NULL;
}
